package in.muton.edushare.poll.sections;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.common.EdusharePanel;
import in.muton.edushare.poll.components.DeleteButton;
import in.muton.edushare.poll.components.DraftButton;
import in.muton.edushare.poll.components.AudienceButton;
import in.muton.edushare.poll.panels.IPollPanelContainer;
import in.muton.edushare.poll.panels.SummaryPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.InnerList;

import java.util.Collections;
import java.util.List;

import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.JavascriptPackageResource;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.PollsDAO;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.PollPages;

public class PollEditPanel extends EdusharePanel {
	private static final long serialVersionUID = -702538263327840832L;
	private IModel<Poll> pollModel;
	
	public PollEditPanel(String id, IModel<Poll> model,
			final EduShareBasePage page, IPollPanelContainer container) {
		super(id, page);
		add(CSSPackageResource.getHeaderContribution(PollEditPanel.class,
				"PollSection.css"));
		add(JavascriptPackageResource.getHeaderContribution(
				PollEditPanel.class, "PollSection.js"));
		pollModel = model;
		if(pollModel.getObject().getStatus() != Poll.POLL_STATUS_DRAFT)
			setError("This poll has already been published and cannot be edited further.");
		
		add(new SummaryPanel("pollDetails", pollModel, page).setVisible(false));
		Form<Void> f;
		add(f = new Form<Void>("pollEditPanelForm"));
		f.add(new ListView<IModel<PollPages>>("pageList",
				new InnerList<PollPages>() {
					private static final long serialVersionUID = 7023698989749292584L;
					@Override
					public List<IModel<PollPages>> getObject() {
						List<PollPages> pageList = pollModel.getObject()
								.getPages();
						Collections.sort(pageList);
						return new CayenneList<PollPages>(pageList).getObject();
					}
				}) {
			private static final long serialVersionUID = 5922903430624318797L;

			@Override
			protected void populateItem(ListItem<IModel<PollPages>> arg0) {
				arg0.add(new PollSectionEdit("page", arg0.getModelObject(),
						page));
			}
		});

		f.add(new AudienceButton("publish", pollModel, page, container));
		f.add(new DraftButton("draft", pollModel, this));
		f.add(new DeleteButton("delete", pollModel, container));
		f.add(new Button("newPage") {
			private static final long serialVersionUID = -6069655090454808654L;
			@Override
			public void onSubmit() {
				new PollsDAO(getORMInterface()).addNewPage(pollModel
						.getObject(), "New Page", PollSectionEdit
						.getMaxPageNum(pollModel.getObject()) + 1);
			}
			@Override
			public boolean isVisible(){
				if (pollModel.getObject().getStatus() == Poll.POLL_STATUS_DRAFT
						&& pollModel.getObject().getCreator() == EduShareSession
								.get().getLoggedInUser())
					return true;
				return false;
			}
		}.setDefaultFormProcessing(false));
	}
}
